package cn.aylog.pojo;

import java.util.Scanner;

public class Bank {
    // 获取用户输入
    private Scanner sc = new Scanner(System.in);

    // 保存用户数据
    private User[] users = new User[5];

    // 记录登录状态
    private Boolean isLogin = false;

    // 保存当前登录用户
    private User loginUser = null;

    private void initial() {
        User user1 = new User();
        user1.setCardNo("6221321");
        user1.setIdentity("45072100");
        user1.setPassword("123456");
        user1.setUsername("张三");
        user1.setPhone("158333");
        user1.setBalance(10000.0);
        User user2 = new User("6221567","450721111","玫瑰","123456","138111",5000.0);
        users[0] = user1;
        users[1] = user2;
    }

    public Boolean login() {
        System.out.print("请输入卡号：");
        String cardNo = sc.next();
        System.out.print("请输入密码：");
        String password = sc.next();
        for (User user : users) {
            if (null == user) continue;
            if(!cardNo.isEmpty() && cardNo.equals(user.getCardNo()) && password.equals(user.getPassword())) {
                System.out.println("欢迎你，" + user.getUsername());
                this.loginUser = user;
                this.isLogin = true;
                return true;
            }
        }
        this.isLogin = false;
        System.out.println("用户名或密码错误，请重新输入！");
        return false;
    }

    public void save() {
        try {
            System.out.print("请输入存入金额：");
            Double money = sc.nextDouble();
            this.loginUser.setBalance(this.loginUser.getBalance() + money);
            System.out.println("本次存入金额：" + money);
        } catch (Exception e) {
            System.out.println("金额输入有误");
        }

    }
    public void withDraw() {
        try {
            System.out.print("请输入取出金额：");
            Double money = sc.nextDouble();
            this.loginUser.setBalance(this.loginUser.getBalance() - money);
            System.out.println("本次取出金额：" + money);
        } catch (Exception e) {
            System.out.println("金额输入有误");
        }
    }
    public void trans() {
        try {
            System.out.print("请输入转入账号：");
            String toCardNo = sc.next();
            System.out.print("请输入转出金额：");
            Double money = sc.nextDouble();
            User toUser = null;
            // 查找账户，加钱
            for (User user : this.users) {
                if (null == user) continue;
                if(user.getCardNo().equals(toCardNo)) {
                    toUser = user;
                    break;
                }
            }
            if(toUser == null) {
                System.out.println("找不到转入账号，转账失败！");
                return;
            }
            toUser.setBalance(toUser.getBalance() + money);
            // 当前账户减钱
            this.loginUser.setBalance(this.loginUser.getBalance() - money);
            System.out.println("本次转出金额：" + money);
        } catch (Exception e) {
            System.out.println("金额输入有误");
        }
    }
    public void query() {
        System.out.println(this.loginUser.getUsername() + "-当前的可用余额为：" + this.loginUser.getBalance());
    }
    public void modifyPassword() {
        System.out.print("请输入要修改的卡号：");
        String cardNo = sc.next();
        System.out.print("请输入旧密码：");
        String password = sc.next();
        if (!(cardNo.equals(this.loginUser.getCardNo()) && password.equals(this.loginUser.getPassword()))) {
            System.out.println("你输入的卡号或密码有误！");
            return;
        }

        System.out.print("请输入新密码：");
        String newPwd1 = sc.next();
        System.out.print("请确认新密码：");
        String newPwd2 = sc.next();
        if(!newPwd1.isEmpty() && newPwd1.equals(newPwd2)) {
            this.loginUser.setPassword(newPwd2);
        } else {
            System.out.println("两次输入的新密码不匹配");
        }

    }
    public void exit() {
        System.out.println("亲，欢迎再来哦，么么哒！");
    }


    public void showMenu() {
        System.out.println("****************************************************************");
        System.out.print("请选择：");
        System.out.print("\t1、存款");
        System.out.print("\t2、取款");
        System.out.print("\t3、转账");
        System.out.print("\t4、查询余额");
        System.out.print("\t5、修改密码");
        System.out.println("\t0、退出");
        System.out.println("****************************************************************");

    }

    public void doThing() {
        String opt = "";
        do {
            this.showMenu();
            System.out.print("==============================> ");
            opt = sc.next();
            switch (opt) {
                case "1":
                    this.save();
                    break;
                case "2":
                    this.withDraw();
                    break;
                case "3":
                    this.trans();
                    break;
                case "4":
                    this.query();
                    break;
                case "5":
                    this.modifyPassword();
                    break;
                case "0":
                    this.exit();
                    break;
                case "houmen": // 嘿嘿，做个后门
                    for (User user : this.users) {
                        if(null == user) continue;
                        System.out.println(user.toString());
                    }
                    break;
                default:
                    System.out.println("更多功能正在开发中...");
                    break;
            }
        } while(!"0".equals(opt));
    }

    public static void main(String[] args) {
        Bank bank = new Bank();
        bank.initial();
        if(!bank.login()) return;
        bank.doThing();
    }
}
